package com.sunshine.hook;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import com.google.gson.Gson;
import com.sunshine.util.LogUtil;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedHelpers;
import org.json.JSONObject;

import static com.sunshine.constant.HookConstant.*;
import static com.sunshine.util.ClassUtil.getClazz;

/**
 * Description:
 * File: ChatHook.java
 * Create: 2019/02/25 15:21
 *
 * @Author: Administrator
 */
class ChatHook {
    private ClassLoader mClassLoader;
    private Context mContext;

    ChatHook(Context context) {
        mContext = context;
        hookInit();
    }

    private void hookInit() {
        XposedHelpers.findAndHookMethod(LTB_MAIN_ACTIVITY_NAME, mContext.getClassLoader(),
                "onCreate", Bundle.class, new XC_MethodHook() {
                    @Override
                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                        super.afterHookedMethod(param);
                        LogUtil.log("LTB_MAIN_ACTIVITY_NAME onCreate");
                        Activity activity = (Activity) param.thisObject;
                        initParam(activity);
                    }
                });
    }

    private void initParam(Activity activity) {
        if (mClassLoader != null) {
            return;
        }
        LogUtil.log("initParam");
        mClassLoader = activity.getClassLoader();
        ltbHook();
    }

    private void ltbHook() {
        getSessionId();
        hookRedPacketResult();
    }

    //可以自己点开红包,查看回调结果
    private void hookRedPacketResult() {
        Class classT = getClazz(LTB_RED_PACKET_T_NAME, mClassLoader);
        XposedHelpers.findAndHookMethod(LTB_RED_PACKET_INFO_C2C_ACTIVITY_INNER_A_NAME, mClassLoader,
                "a", Context.class, classT, boolean.class, boolean.class, new XC_MethodHook() {
                    @Override
                    protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                        super.afterHookedMethod(param);
                        try {
                            //获取 上述a 方法的第二个参数
                            Object v1 = param.args[1];
                            String str = new Gson().toJson(v1);
                            JSONObject object = new JSONObject(str);
                            LogUtil.log("hookRedPacketResult ：" + str);
                            String remark = object.optString("c");
                            String money = object.optString("d");
                            LogUtil.log("hookRedPacketResult " + String.format("remark = %s,money = $s", remark, money));
                            //0 原始包未拆  1 原始包已拆
                        } catch (Exception e) {
                            LogUtil.log("a Exception : " + e.toString());
                            e.printStackTrace();
                        }
                    }
                });

    }

    //在发送红包那个界面,获取SessionId
    private void getSessionId() {
        Class getSessionId = XposedHelpers.findClass(LTB_SEND_RED_PACKET_ACTIVITY_NAME, mClassLoader);
        XposedHelpers.findAndHookMethod(getSessionId, "getSessionId", new XC_MethodHook() {
            @Override
            protected void afterHookedMethod(MethodHookParam param) throws Throwable {
                super.afterHookedMethod(param);
                String result = (String) param.getResult();
                LogUtil.log("getSessionId result = " + result);
            }
        });
    }
}
